package com.btex.app.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.btex.app.R;
import com.btex.app.entity.ShiDangInfo;
import com.btex.app.tools.LogUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wcx on 16/3/20.
 */
public class ShiDangAdapter extends BaseAdapter{
    private Context cxt;
    private List<ShiDangInfo> data = new ArrayList<>();
    private String TAG = "ShiDangAdapter";
    private boolean isBuy;

    public ShiDangAdapter(Context cxt,boolean isBuy) {
        this.cxt = cxt;
        this.isBuy = isBuy;
    }

    @Override
    public int getCount() {
        return data==null?0:data.size();
    }

    public void addData(boolean clear, List<ShiDangInfo> shiDangInfos){
        if(clear){
            data.clear();
            LogUtil.i(TAG, "刷新数据");
        }
        data.addAll(shiDangInfos);
        notifyDataSetChanged();
    }

    @Override
    public Object getItem(int position) {
        if(isBuy)
            return (data==null||data.size()==0)?null : (data.size()>=position?data.get(position):null);
        else
            return (data==null||data.size()==0)?null : (data.size()>=position?data.get(data.size()-position-1):null);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(cxt).inflate(
                    R.layout.item_shidang_entrust, null);
            holder = new ViewHolder();
            holder.tvBuySell = (TextView) convertView.findViewById(R.id.tv_buy_sell);
            holder.tvBuyPrice = (TextView) convertView.findViewById(R.id.tv_buyPrice);
            holder.tvBuyNum = (TextView) convertView.findViewById(R.id.tv_buyNum);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        ShiDangInfo shiDangInfo = (ShiDangInfo) getItem(position);
        if(shiDangInfo!=null) {
            holder.tvBuyPrice.setText(shiDangInfo.getPrice());
            holder.tvBuyNum.setText(shiDangInfo.getAmount());
        }

        if(isBuy){
            holder.tvBuySell.setText(cxt.getString(R.string.buy)+(position+1));
            holder.tvBuySell.setTextColor(cxt.getResources().getColor(R.color.tv_red));
            holder.tvBuyPrice.setTextColor(cxt.getResources().getColor(R.color.tv_red));
            holder.tvBuyNum.setTextColor(cxt.getResources().getColor(R.color.tv_red));
        }else{
            holder.tvBuySell.setText(cxt.getString(R.string.sell)+(data.size()-position));
        }

        return convertView;
    }


    private static class ViewHolder{
        private TextView tvBuySell;
        private TextView tvBuyPrice;
        private TextView tvBuyNum;
    }
}
